const Mock = require('mockjs');
const express = require('express')
const app = express()
const port = 3000
const Random = Mock.Random; // Mock.Random 是一个工具类，用于生成各种随机数据
app.all('*', (req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header("Access-control-Allow-Headers", "xCors");    //允许请求头中携带 xCors
    next();
});

app.get('/api/lists', (req, res) => {
    res.send(Mock.mock(() => {
        let i = 0,
        _data = [],
        page = req.query.page, //当前页数
        pageSize = req.query.num, //每次返回的条数
        len = (1000 - pageSize * (page - 1)) < pageSize ? (1000 - pageSize * (page - 1)) : pageSize;
        for (i; i < len; i++) {
            let newArticleObject = {
                title: Random.csentence(5, 30), //  Random.csentence( min, max )
                thumbnail_pic_s: Random.image('88x64', '#50B347', '#FFF', 'Mock.js'), // Random.image( size, background, foreground, format, text )
                author_name: Random.cname(), // Random.cname() 随机生成一个常见的中文姓名
                date: Random.date() + " " + Random.time(), // Random.date()指示生成的日期字符串的格式,默认为yyyy-MM-dd；Random.time() 返回一个随机的时间字符串
            };
            _data.push(newArticleObject);
        }
        return {
          code: "0",
          message: "success",
          data: {
            page: page,
            num: pageSize,
            lists: _data
          },
        };
      }))
    // res.send(lists);
})

app.listen(port, () => console.log(`Example app listening on port ${port}!`))